package com.digital.herbarium.Controller;


import com.digital.herbarium.annotation.CheckToken;
import com.digital.herbarium.Entity.ResponseResult;
import com.digital.herbarium.Entity.User;
import com.digital.herbarium.annotation.Log;
import com.digital.herbarium.service.UserServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServices userServices;

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @GetMapping("/getUserInfo")
    @CheckToken()
    @Log("user")
    public ResponseResult getUserInfo(User user, @RequestParam(value = "pageSize",defaultValue = "10",required = false) int pageSize,
                                      @RequestParam(value = "pageNum",defaultValue = "0",required = false) int pageNum){
       // List<User> ulist = userServices.getUserInfo(user);
        return ResponseResult.SUCCESS().setData( userServices.getUserInfo(user,pageSize,  pageNum));
    }

    @PostMapping("/addUser")
    @CheckToken()
    public ResponseResult addUser(User user){
       userServices.addUser(user);
       return   ResponseResult.SUCCESS().setData("添加成功");
    }

    @PostMapping("/editUser")
    @CheckToken()
    public ResponseResult editUser(User user){
        userServices.editUser(user);
        return   ResponseResult.SUCCESS().setData("更新成功");
    }

    @GetMapping("/deltUser")
    @CheckToken()
    public ResponseResult deltUser(String id){
        userServices.deltUser(id);
        return ResponseResult.SUCCESS().setData("删除成功");
    }

    @GetMapping("enableUser")
    @CheckToken()
    public  ResponseResult enableUser(Integer id,Integer state){
        userServices.enableUser(id, state);
        return ResponseResult.SUCCESS().setData("操作成功");
    }

    @GetMapping("resetPwd")
    @CheckToken()
    public  ResponseResult resetPwd(Integer  id,String password){
        userServices.resetOldPwd(id, password);
        return ResponseResult.SUCCESS().setData("操作成功");
    }

    @PostMapping("editPwd")
    @CheckToken()
    public  ResponseResult editPwd(String  useraccount,String password,String newpwd){
        userServices.resetPwd(useraccount, password, newpwd);
        return ResponseResult.SUCCESS().setData("操作成功");
    }

    @PostMapping("login")
    public ResponseResult login(String useraccount,String password){

        return ResponseResult.SUCCESS().setData(userServices.login(useraccount,password));
    }

    @GetMapping("changeToAdmin")
    public ResponseResult changeToAdmin(String useraccount){
        userServices.changeToAdmin(useraccount);
        return ResponseResult.SUCCESS().setData("更改成功");
    }
}
